//
//  RZViewController.swift
//  Pods
//
//  Created by eme on 2017/7/26.
//
//

import UIKit
import UtilCore
import BrandonCore

class RZViewController: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var customTableView: UITableView!        //商品列表
    var detailid:String?
    var LoginUserVm:GetLoginUserVm?
    
    var shopVm:shopMessageVm?
    
    var messageModel:ShopMseeageModel?{
        didSet{
            self.customTableView.reloadData()
        }
    }
    /*----------------------------   自定义属性区域    ----------------------------*/
    
    
    /****************************Storyboard 绑定方法区域****************************/
    
    @IBAction func buttonClicked(_ sender: UIButton) {
        _ = "authinfopagevc".openURL(["spid":AppData.loginedUser?.glShop.spid ?? ""])
    }
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.LoginUserVm?.getLoginUser.onNext()
        self.shopVm?.getShopMessage.onNext(AppData.loginedUser?.glShop.spid ?? "")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.customTableView.estimatedRowHeight = 200
        self.customTableView.rowHeight = UITableViewAutomaticDimension
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.LoginUserVm = GetLoginUserVm()
        
        self.LoginUserVm?.backData.asObserver().subscribe(onNext:{ user in
            
            self.customTableView.reloadData()
            
        }).addDisposableTo(disposeBag)
        
        self.shopVm = shopMessageVm()
        
        self.shopVm?.backMessage.subscribe(onNext:{ result in
            
            self.messageModel = result
            
        }).addDisposableTo(disposeBag)
        
        self.shopVm?.loading.asObservable().bindTo(self.view.rx_loading).addDisposableTo(disposeBag)
        
        self.shopVm?.error.asObserver().bindTo(self.view.rx_error).addDisposableTo(disposeBag)
    }
}
extension RZViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 0
        case 1:
            return 5
        case 2:
            if case AppDataState.auditfailed = AppData.appState{
                return 3
            }else{
                return 2
            }
        case 3:
            if case .auditfailed = AppData.appState{
                return 1
            }else{
                return 0
            }
        default:
            return 0
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 1 && indexPath.row == 4{
            let cell = tableView.dequeueReusableCell(withIdentifier: "yyzzimage", for: indexPath) as! RZ_yyzzImageTableViewCell
            return cell
        }else if indexPath.section == 3{
            let cell = tableView.dequeueReusableCell(withIdentifier: "button", for: indexPath) as! RZ_buttonTableViewCell
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "message", for: indexPath) as! RZ_MessageTableViewCell
            cell.titleLabel.text = [[""],["企业名称","联系人姓名","所在省市区","营业执照号",""],["申请时间","审核状态","不通过原因"],[""]][indexPath.section][indexPath.row]
            if let user = AppData.loginedUser{
                cell.messageLabel.text = [[""],[user.glShop.spcompany,user.glShop.spcontact,"\(user.glShop!.agprovincename ?? "")\(user.glShop!.spareaname ?? "")\(user.glShop!.agareaname ?? "")",user.glShop.splicense,""],[user.glShop.spregdate,user.glShop.showSpstate == "审核通过" ? "已认证":user.glShop.showSpstate,self.messageModel?.auditremark],[""]][indexPath.section][indexPath.row]
            }
            return cell
        }
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "rzstatus") as! RZ_StatusTableViewCell
            return cell.contentView
        case 1,2:
            let cell = tableView.dequeueReusableCell(withIdentifier: "rzheader") as! RZ_HeaderTableViewCell
            cell.titleLable.text = ["","认证资料","审核信息",""][section]
            return cell
        default:
            return nil
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 1:
            if indexPath.row == 4{
                return 120
            }else{
                return 39
            }
        case 2:
            if indexPath.row == 2{
                return tableView.rowHeight
            }else{
                return 39
            }
        case 3:
            return 143
        default:
            return 0
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return 31
        case 1,2:
            return 40
        default:
            return 0.01
        }
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
}
